package com.byron.media.server.rtsp.model;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;

public class LineDelimiter {

    public static final LineDelimiter DEFAULT;

    static {
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        PrintWriter out = new PrintWriter(bout);
        out.println();
        DEFAULT = new LineDelimiter(new String(bout.toByteArray()));
    }

    public static final LineDelimiter AUTO = new LineDelimiter("");

    public static final LineDelimiter UNIX = new LineDelimiter("\n");

    public static final LineDelimiter WINDOWS = new LineDelimiter("\r\n");

    public static final LineDelimiter MAC = new LineDelimiter("\r");

    private final String value;

    public LineDelimiter(String value) {
        if (value == null) {
            throw new NullPointerException("delimiter");
        }
        this.value = value;
    }

    public String getValue() {
        return value;
    }

    public int hashCode() {
        return value.hashCode();
    }

    public boolean equals(Object o) {
        if (!(o instanceof LineDelimiter)) {
            return false;
        }

        LineDelimiter that = (LineDelimiter) o;
        return this.value.equals(that.value);
    }

    public String toString() {
        StringBuffer buf = new StringBuffer();
        buf.append("delimiter:");
        if (value.length() == 0) {
            buf.append(" auto");
        } else {
            for (int i = 0; i < value.length(); i++) {
                buf.append(" 0x");
                buf.append(Integer.toHexString(value.charAt(i)));
            }
        }
        return buf.toString();
    }

}
